#pragma once

#include <iostream>
#include <string>
#include <functional>
#include "Reactor.hpp"
#include "InitAddr.hpp"

class Reactor;

// 管理连接的类
class Connection
{
public:
	Connection() : _sockfd(-1), _events(0)
	{
	}
	~Connection()
	{
	}
	// 获取客户端信息
	void Setpeeraddr(Inetaddr peer_addr)
	{
		_peer_addr = peer_addr;
	}
	int Sockfd() { return _sockfd; }
	void Setevents(uint32_t events) { _events = events; }
	void Setsockfd(int sockfd) { _sockfd = sockfd; }
	uint32_t GetEvents() { return _events; }
	void SetOwner(Reactor *owner)
	{
		_owner = owner;
	}
	Reactor *GetOwner()
	{
		return _owner;
	}
	void Append(const std::string &in)
	{
		_inbuffer += in; // 把收到的数据，添加到自己的接受缓冲区
	}
	void AppendToOut(const std::string &out)
	{
		_outbuffer += out;
	}
	void DisCardOutString(int n)
	{
		_outbuffer.erase(0, n);
	}
	bool IsOutBufferEmpty() { return _outbuffer.empty(); }
	std::string &OutString() // for test
	{
		return _outbuffer;
	}
	std::string &InBuffer() // 故意这样写
	{
		return _inbuffer;
	}
	void Close()
    {
        if(_sockfd>=0)
            close(_sockfd);
    }
	// 回调方法
	virtual void Sender() = 0;
	virtual void Recver() = 0;
	virtual void Excepter() = 0;

private:
	int _sockfd;			// socket 文件描述符
	std::string _inbuffer;	// 输入缓冲区
	std::string _outbuffer; // 输出缓冲区
	Inetaddr _peer_addr;	// 对应的客户端

	// 添加一个回指的指针
	Reactor *_owner;

	// 关系的事件我这个connection关心的事件
	uint32_t _events;
};